var rootview = ui("$");
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var global = sm("do_Global");
var common = require("common");
var openHelper = require("helper/open");
var http = require("helper/http");
var pubUrl = require("url/pub");
var bizzType = require("helper/bizzType");
var communityUtil = require("helper/community");

var layoutComment = ui("layoutComment");
var praiseLayout = ui("praiseLayout");
var lineLayout = ui("lineLayout");
var commentLayout = ui("commentLayout");

//图片展示gridView
var do_GridView_1 = ui("do_GridView_1");
var do_GridView_2 = ui("do_GridView_2");
var listdata4Comment = mm("do_ListData");
do_GridView_2.bindItems(listdata4Comment);

var InforZan = ui("InforZan");
rootview.setMapping({   // 此处的Mapping只能设置在 RootView 上 ;
    "UserName.text" : "creatorName",
    "UserPic.tag" : "creatorId",
    "UserPic.source" : "fullCreatorImg",
    "InforDescribe.text" : "content",
    "InforTime.text" : "cnNewAt",
    "InforReply.text" : "commentCont",
    "InforZan.text" : "praiseCont",
    "do_ImageView_5.tag" : "isPraised",
    "do_GridView_1.items" : "imgItems",
    "do_GridView_1.tag" : "bigImgItems",
    "do_GridView_1.visible" : "hasImg",
    "do_ALayout_4.tag" : "id",
   // "do_ALayout_6.visible": "isShowPraiseList",
    "praiseList.text" :"praiseUserList",
    "do_GridView_2.tag" :"commentList",
    "commentLayout.visible" : "isShowComment"
});

rootview.on("dataRefreshed", function(){
	listdata4Comment.removeAll();
	showPraiseList();
	//评论信息
	listdata4Comment.addData(JSON.parse(do_GridView_2.tag));
	do_GridView_2.refreshItems();
	
	if(ui("do_ImageView_5").tag == 1){
		ui("do_ImageView_5").source = "source://image/common/praise1.png";
	}else{
		ui("do_ImageView_5").source = "source://image/common/praise.png";
	}
});

//图片浏览注册事件
do_GridView_1.on("touch", function(data){
	openHelper.open("source://view/reuse/imgBrowser.ui", {
		imgDatas :do_GridView_1.tag,index:data
	});
});

//点击个人头像-他的人个主页
ui("alayoutUserInfo").on("touch", function(data, e) {
	var userId = ui("UserPic").tag;
	if(userId !=null){
		openHelper.openPersonal(userId);
	}
	
});

//点击评论按钮
ui("do_ALayout_4").on("touch", function(data, e) {
	var topicId = ui("do_ALayout_4").tag;
	openHelper.open("source://view/public/comment/main.ui",{"bizzId":topicId,"bizzType":bizzType.Level2.CIRCLE_TOPIC});
});

//点赞按钮
ui("do_ALayout_5").on("touch", function(data, e) {
	var topicId = ui("do_ALayout_4").tag;
	var addParams = {
            bizzId:topicId,
            bizzType:bizzType.Level2.CIRCLE_TOPIC,
            communityId:communityUtil.communityVisitId()
		};
	if(ui("do_ImageView_5").tag == 1){
		//如果已点赞，再次点击则取消点赞
		ui("do_ImageView_5").source = "source://image/common/praise.png";
		ui("do_ImageView_5").tag = 2;
		var count = parseInt(ui("InforZan").text);
		if(count > 0){
			ui("InforZan").text = count - 1;
		}		
		http.post(pubUrl.url.praise.doCancle,addParams,function(data){
			ui("praiseList").text = data.praiseUserList; 
			 showPraiseList();
		});
	}else{
		ui("do_ImageView_5").source = "source://image/common/praise1.png";
		ui("do_ImageView_5").tag = 1;
		var count = parseInt(ui("InforZan").text);
		ui("InforZan").text = count + 1;
		http.post(pubUrl.url.praise.doAdd,addParams,function(data){
			ui("praiseList").text = data.praiseUserList; 
			 showPraiseList();
		});
	}
});

function showPraiseList(){
	if(ui("praiseList").text&&commentLayout.visible){ //点赞+评论
		commentLayoutBox(3);
	}
	else if(!ui("praiseList").text&&commentLayout.visible){ //只有评论
		commentLayoutBox(2);
	}
	else if(ui("praiseList").text&&!commentLayout.visible){ //只有点赞
		commentLayoutBox(1);
	}
	else{  //无点赞无评论
		commentLayoutBox(0);
	}
}

function commentLayoutBox(data){
	var array = [[false,false,false,false],[true,true,false,false],[true,false,false,true],[true,true,true,true]];
	layoutComment.visible = array[data][0];
	praiseLayout.visible = array[data][1];
	lineLayout.visible = array[data][2];
	commentLayout.visible = array[data][3];
}